/*
 *    Copyright (c) 2023 李伟国
 *    moyu is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */

package cn.moyu.service.impl;

import cn.moyu.conf.page.PageFactory;
import cn.moyu.entity.KettleDatabaseInfo;
import cn.moyu.mapper.KettleDatabaseInfoMapper;
import cn.moyu.service.IKettleDatabaseInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.springframework.stereotype.Service;


/**
 * 数据源信息
 */

@Service
public class KettleDatabaseInfoServicesImpl extends ServiceImpl<KettleDatabaseInfoMapper, KettleDatabaseInfo> implements IKettleDatabaseInfoService {

    /**
     * 获取数据源列表
     *
     * @param name 数据源名称
     * @return 结果
     */
    @Override
    public Page<KettleDatabaseInfo> getListPage(String name) {
        return this.baseMapper.selectListPage(PageFactory.defaultPage(), name);
    }

    /**
     * 连接测试
     *
     * @param databaseInfo 数据源信息
     * @return 结果
     */
    @Override
    public String connectTest(KettleDatabaseInfo databaseInfo) {
        DatabaseMeta databaseMeta = new DatabaseMeta();
        databaseMeta.setName(databaseInfo.getName());
        databaseMeta.setHostname(databaseInfo.getHost());
        databaseMeta.setAccessType(DatabaseMeta.TYPE_ACCESS_NATIVE);
        databaseMeta.setDatabaseType(databaseInfo.getType());
        databaseMeta.setDBPort(databaseInfo.getPort());
        databaseMeta.setDBName(databaseInfo.getDb());
        databaseMeta.setUsername(databaseInfo.getUser());
        databaseMeta.setPassword(databaseInfo.getPassword());
        return databaseMeta.testConnection();
    }
}
